<?php

    if (!class_exists('mdl_productCat'))
     {
         $model_dir =  ((!defined('SHOP_DEVELOPER') || !constant('SHOP_DEVELOPER')) && version_compare(PHP_VERSION,'5.0','>=')?'model_v5':'model');
         require_once(CORE_DIR.'/'.$model_dir.'/goods/mdl.productCat.php');
     }

     class mdl_diamondCat extends mdl_productCat
     {
         function mdl_diamondCat()
         {
             parent::mdl_productCat();
         }
         

         function _addNew($data)
         {
            $oTemplate = $this->system->loadModel('system/template');
            $data['parent_id'] = intval($data['parent_id']);
            $data['addon']['meta']['title'] = htmlspecialchars($data['title']);
            $data['addon']['meta']['keywords'] = htmlspecialchars($data['keywords']);
            $data['addon']['meta']['description'] = htmlspecialchars($data['description']);
            $parent_id = $data['parent_id'];
            $path=array();
            while($parent_id){
                if($data['cat_id'] && $data['cat_id'] == $parent_id){
                    return false;
                    break;
                }
                array_unshift($path, $parent_id);
                $row = $this->db->selectrow('SELECT parent_id, cat_path, p_order FROM sdb_goods_cat WHERE cat_id='.intval($parent_id));
                $parent_id = $row['parent_id'];
            }
            $data['cat_path'] = implode(',',$path).',';
            $oseo = &$this->system->loadModel('system/seo');
            $aData=array(
                'keywords'=>$data['addon']['meta']['keywords'],
                'descript'=>$data['addon']['meta']['description'],
                'title'=>$data['addon']['meta']['title']
            );

            if($data['cat_id']){
                $oseo->set_seo('goods_cat',$data['cat_id'],$aData);
                $sDefine=$this->db->selectrow('SELECT parent_id FROM sdb_goods_cat WHERE cat_id='.intval($data['cat_id']));
                $rs = $this->db->exec('SELECT * FROM sdb_goods_cat WHERE cat_id='.$data['cat_id']);
                $sql = $this->db->getUpdateSQL($rs,$data);


                if(!$sql || $this->db->exec($sql)){

                    if($sDefine['parent_id']!=$data['parent_id']){

                        /*
                        $rs = $this->db->exec('SELECT * FROM sdb_goods_cat WHERE parent_id='.$data['cat_id']);
                        $sql =$this->db->getUpdateSQL($rs,array('parent_id'=>$sDefine['parent_id']));
                        $this->db->exec($sql);
                        */

                        $this->updatePath($data['cat_id'],$data['cat_path']);
                        $this->updateChildCount($sDefine['parent_id']);
                        $this->updateChildCount($data['parent_id']);
                    }
                    $oTemplate->update_template('cat',$data['cat_id'],$_POST['goodscat_template'],'gallery');
                    $oTemplate->update_template('cat',$data['cat_id'],$_POST['product_template'],'product');
                    $this->cat2json();
                    return $data['cat_id'];
                }else{
                    return false;
                }
            }else{
                unset($data['cat_id']);
                $rs = $this->db->exec('SELECT * FROM sdb_goods_cat WHERE 0=1');
                $sql = $this->db->getInsertSQL($rs,$data);
                if(!$sql || $this->db->exec($sql)){
                    $cat_id=$this->db->lastInsertId();
                    $oseo->set_seo('goods_cat',$cat_id,$aData);
                    $oTemplate->set_template('cat',$cat_id,$_POST['goodscat_template'],'gallery');
                    $oTemplate->set_template('cat',$cat_id,$_POST['product_template'],'product');
                    $this->updateChildCount($data['parent_id']);
                    $this->cat2json();
                    return $cat_id;
                }else{
                    return false;
                }
            }
         }

         function addNew($data)
         {
             //error_log(print_r($data,true),3,__FILE__.'.'.__FUNCTION__.'.data.log');//alone add
             //裸钻默认分类
            $diamond_cat_tpl_bool = $data['diamond_cat_tpl']?true:false;
            unset($data['diamond_cat_tpl']);
            
            //钻托默认分类
            $endstone_filter_cat_bool = $data['endstone_filter_cat']?true:false;
            unset($data['endstone_filter_cat']);

            $return = $this->_addNew($data);
            /* alone add start */
            //裸钻定制分类页
            $diamond_tpl_cat = $this->system->getConf('diamond.diamond_cat_tpl');
            if ($return)
            {
                if ($diamond_cat_tpl_bool && ($data['disabled'] == 'false'))
                {
                    $this->system->setConf('diamond.diamond_cat_tpl',$return);
                }
                else
                {
                    if ($diamond_tpl_cat == $return)
                    {
                        $this->system->setConf('diamond.diamond_cat_tpl','');
                    }
                }

                //钻托setting
                $endstone_filter_cat = $this->system->getConf('diamond.endstone_filter_cat');
                if ($endstone_filter_cat_bool && ($data['disabled'] == 'true'))
                {
                    $this->system->setConf('diamond.endstone_filter_cat',$return);
                }
                else
                {
                    if ($endstone_filter_cat == $return)
                    {
                        $this->system->setConf('diamond.endstone_filter_cat','');
                    }
                }
            }

            if ($data['cat_id'] && ('true'==$data['disabled']))
            {
                $aChildrenCat = $this->getChildrenCat($data['cat_id']);
                if (!empty($aChildrenCat))
                {
                    $sql = 'update sdb_goods_cat set disabled="true" where cat_id in ('.implode(',',$aChildrenCat).')';
                    return  $return && $this->db->exec($sql);
                }
            }
            /* alone add end */
            return $return;
         }

         function getChildrenCat($cat_id)
         {
             $sql = 'SELECT cat_id,parent_id,cat_path FROM sdb_goods_cat WHERE parent_id!="0" and disabled="false"';
             $aRow = $this->db->select($sql);
             $aChildrenCat = array();
             foreach ($aRow as $k => $v)
             {
                 $aPath = explode(',',trim($v['cat_path']));
                 if (in_array($cat_id,$aPath))
                 {
                     $aChildrenCat[] = $v['cat_id'];
                 }
             }
             return $aChildrenCat;
         }

         function getDisabledCat($disabled=true)
         {
             if ($disabled)
             {
                 $where = 'disabled="true"';
             }
             else $where = 'disabled="false"';
             $sql = 'SELECT cat_id FROM sdb_goods_cat WHERE '.$where;
             $aDisabledCat = $this->db->select($sql);
             foreach ($aDisabledCat as $key =>$var)
             {
                $aDisabledCat[$key] = $var['cat_id'];
             }
             return $aDisabledCat;
         }
     }
?>